﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;

namespace Clipper.DataManage
{
    /// <summary>
    /// WinResultReferenceInput.xaml 的交互逻辑
    /// </summary>
    public partial class WinResultReferenceInput : BaseWindow
    {
        public WinResultReferenceInput()
        {
            InitializeComponent();
          
        }

        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {

            BLL.DetectItems detectItems = new DetectItems();

            BLL.QualitativeFormatInfo qualitativeformatInfo = new QualitativeFormatInfo();
            qualitativeFormat.ItemsSource = qualitativeformatInfo.GetModelList("");

            if (BaseServiceProvider.InputName == Model.InputEnum.Update)
            {
                DetectItem.ItemsSource = detectItems.GetModelList("");
                Model.ResultReference model = BaseServiceProvider.DataModel as Model.ResultReference;
                if (model != null)
                {
                    DetectItem.IsEnabled = false;
                    DataContext = model;
                }
            }
            else
            {
                DetectItem.ItemsSource = detectItems.GetResultReferenceList();
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            bool result = true;
            if (Check())
            {
                Clipper.Model.ResultReference model = new Clipper.Model.ResultReference();
                Clipper.BLL.ResultReference resultreference = new BLL.ResultReference();
                model.DetectItem = Convert.ToString(DetectItem.SelectedValue);
                model.RRsToplimit = RRsToplimit.Number;
                model.RRsLowlimit = RRsLowlimit.Number;
                model.HighLimit = HighLimit.Number;
                model.LowLimit = LowLimit.Number;
                model.unit = unit.Text;
                model.qualitativeA = qualitativeA.Number;
                model.qualitativeB = qualitativeB.Number;
                model.qualitativeC = qualitativeC.Number;
                model.qualitativeD = qualitativeD.Number;
                model.QualitativeFormat = Convert.ToInt32(qualitativeFormat.SelectedValue);
                model.negativeLight = negativeLight.Number;
                model.positiveLight = positiveLight.Number;
                model.note = note.Text;
                if (BaseServiceProvider.InputName == Model.InputEnum.Add)
                {
                    result= resultreference.Add(model);
                }
                else
                {
                    result= resultreference.Update(model);
                }
                
                    this.DialogResult = result;
                
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = null;
            this.Close();
        }

        private bool Check() 
        {
            List<string> strErr = new List<string>();

            if (DetectItem.SelectedIndex < 0)
            {
                strErr.Add("DetectItemsNotNullTip");
            }

            if (string.IsNullOrEmpty(RRsToplimit.Text))
            {
                strErr.Add("RRsTopLimitNotNullTip"); 
            }

            if (string.IsNullOrEmpty(RRsLowlimit.Text))
            {
                strErr.Add("RRsLowLimitNotNullTip");
            }

            if (string.IsNullOrEmpty(HighLimit.Text))
            {
                strErr.Add("HighLimitNotNullTip");
            }

            if (string.IsNullOrEmpty(LowLimit.Text))
            {
                strErr.Add("LowLimitNotNullTip");
            }

            if (string.IsNullOrEmpty(unit.Text))
            {
                strErr.Add("UnitNotNullTip");
            }

            if (string.IsNullOrEmpty(qualitativeA.Text))
            {
                strErr.Add("QualitativeANotNullTip");
            }

            if (string.IsNullOrEmpty(qualitativeB.Text))
            {
                strErr.Add("QualitativeBNotNullTip");
            }

            if (string.IsNullOrEmpty(qualitativeC.Text))
            {
                strErr.Add("QualitativeCNotNullTip");
            }

            if (string.IsNullOrEmpty(qualitativeD.Text))
            {
                strErr.Add("QualitativeDNotNullTip");
            }

            if (qualitativeFormat.SelectedIndex < 0)
            {
                strErr.Add("QualitativeFormatNotNullTip");
            }
            if (string.IsNullOrEmpty(negativeLight.Text))
            {
                strErr.Add("NegativeLightNotNullTip");
            }

            if (string.IsNullOrEmpty(positiveLight.Text))
            {
                strErr.Add("PositiveLightNotNullTip");
            }
            if (strErr.Count>0)
            {
                UIHelper.ShowKeyMessage(strErr);
                return false;
            }
            return true;
        }

    }
}
